package com.zmn.mcc.cas.server;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.zmn.mcc.model.entity.staff.McStaff;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author nowind
 * @since 2017/8/15 17:33
 */
@Deprecated
@Component
public class PermitCacheImpl implements PermitCache {

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public Integer getLoginFailTimes(String staffId) {
        String redisKey = CasServerConsts.MC_LOGIN_FAIL_TIMES + staffId;

        Object data = redisTemplate.opsForValue().get(redisKey);
        if (data == null) {
            return 0;
        }
        if (data instanceof Number) {
            return ((Number) data).intValue();
        }
        return NumberUtils.toInt(data.toString(), 0);
    }

    @Override
    public void setLoginFailTimes(String staffId) {

        String redisKey = CasServerConsts.MC_LOGIN_FAIL_TIMES + staffId;

        redisTemplate.opsForValue().increment(redisKey);
        redisTemplate.expire(redisKey, CasServerConsts.LOGIN_LOCK_TIMESPAN, TimeUnit.SECONDS);
    }

    @Override
    public void removeLoginFailTimes(String staffId) {
        String redisKey = CasServerConsts.MC_LOGIN_FAIL_TIMES + staffId;
        redisTemplate.delete(redisKey);
    }

//    @Override
//    public McStaff setStaffToken(int staffId) {
//        StaffLoginQuery query = new StaffLoginQuery();
//        query.setStaffId(staffId);
//        Staff token = staffDao.findByLogin(query);
//        return this.setStaffToken(token);
//    }

    @Override
    public McStaff setStaffToken(McStaff token) {

        if (token == null) {
            return null;
        }

        String redisKey = CasServerConsts.MC_LOGIN_STAFF + token.getStaffId();
        SimplePropertyPreFilter filter = this.buildJsonFilter();
        redisTemplate.opsForValue().setIfAbsent(redisKey, JSONObject.toJSONString(token, filter), CasServerConsts.DEFAULT_EXPIRED, TimeUnit.SECONDS);

        return token;
    }

    /**
     * 构建忽略的字段filter
     *
     * @return
     */
    private SimplePropertyPreFilter buildJsonFilter() {
        SimplePropertyPreFilter filter = buildBaseJsonFilter();
        Set<String> excludes = filter.getExcludes();

        excludes.add("creater");
        excludes.add("createTime");
        excludes.add("updater");
        excludes.add("updateTime");

        excludes.add("statusCode");
        excludes.add("message");

        return filter;
    }

    /**
     * 构建忽略序列化的字段filter
     *
     * @return
     */
    protected SimplePropertyPreFilter buildBaseJsonFilter() {
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
        Set<String> excludes = filter.getExcludes();

        excludes.add("operatorIP");
        excludes.add("operatorType");
        excludes.add("operatorOrgId");
        excludes.add("operatorId");
        excludes.add("operator");
        excludes.add("operatorRemark");
        excludes.add("auto");
        excludes.add("super");
        excludes.add("token");
        excludes.add("sign");
        excludes.add("fromType");

        return filter;
    }

}
